package com.yangcangclub.merchandise.service.impl;

import com.google.common.collect.Maps;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.merchandise.base.MerchandiseBaseService;
import com.yangcangclub.merchandise.bean.Product;
import com.yangcangclub.merchandise.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商品模块service 实现类
 * Created by 肖明明 on 2017/2/20.
 */
@Service
public class ProductServiceImpl extends MerchandiseBaseService implements ProductService{

    /** 添加商品 */
    @Override
    @Transactional
    public int addProduct(String productName,String category,BigDecimal unitPrice,
                          BigDecimal showUnitPrice,BigDecimal shipping,
                          int inventory, String bannerUrl, String content,int status,
                          int userId,String userName,
                        int isFuxi,int isEveryday,int isConcentrate,int isLimit,
                          String startTime,String endTime,int showInventory,String subtitle,int warehouseType) {
        Product pd=new Product();
        pd.setName(productName);
        pd.setBannerUrl(bannerUrl);
        pd.setContent(content);
        pd.setCategory(category);
        pd.setUnitPrice(unitPrice);
        pd.setShowUnitPrice(showUnitPrice);
        pd.setShipping(shipping);
        pd.setInventory(inventory);
        pd.setStatus(status);
        pd.setOrderSort(0);
        pd.setPubUserId(userId);
        pd.setPubUserName(userName);
        pd.setSubtitle(subtitle);
        int id= productBaseDomain.addProduct(pd);
        //初始化orderSort字段
        productBaseDomain.updateOrderSort(id,id);
        int isShowModule = limitProductModuleStatus()?0:1;
        productBaseDomain.updateProductExtInfo(id,isFuxi,isEveryday,isConcentrate,isLimit,startTime,endTime,showInventory,warehouseType,isShowModule);
        return id;
    }
    
    /** 修改商品信息*/
    @Override
    @Transactional
    public int updateProductInfo(int pid,String productName,String category,BigDecimal unitPrice,BigDecimal showUnitPrice,
                                 BigDecimal shipping, String bannerUrl, String content,
    int isFuxi,int isEveryday,int isConcentrate,int isLimit,
                                 String startTime,String endTime,int showInventory,String subtitle,int warehouseType) {
        Product pd=productBaseDomain.getProductById(pid);
        if(pd==null) throw new ApolloException(MessageEnum.PRODUCT_NOT_EXIST);
        pd.setName(productName);
        pd.setBannerUrl(bannerUrl);
        pd.setContent(content);
        pd.setCategory(category);
        pd.setUnitPrice(unitPrice);
        pd.setShowUnitPrice(showUnitPrice);
        pd.setShipping(shipping);
        pd.setSubtitle(subtitle);
//        pd.setOrderSort(orderSort);
        productBaseDomain.updateProductExtInfo(pid,isFuxi,isEveryday,isConcentrate,isLimit,startTime,endTime,showInventory,warehouseType,pd.getIsShowModule());
        return productBaseDomain.updateProductInfo(pd);
    }

    /** 获取商城商品列表，有排序*/
    @Override
    public List<Product> getShowProductList(String category,int isLimit,int orderSort,int pageSize) {
        return productBaseDomain.getShowProductList(category,isLimit,orderSort,pageSize);
    }

    /** 根据ID列表获取商品概要信息列表*/
    @Override
    public List<Product> getProductListByIds(List<Integer> ids) {
        return productBaseDomain.getProductListByIds(ids);
    }

    /** 根据ID获取商品详情*/
    @Override
    public Product getProductById(int id) {
        return productBaseDomain.getProductById(id);
    }

    /** 后台获取商品信息分页列表*/
    @Override
    public Map<String,Object> getPagenationProductList(int pageNum, int pageSize,Map map) {
        pageNum=pageNum>1?pageNum:1;
        int start=(pageNum-1)*pageSize;
        return productBaseDomain.getPagenationProductList(start,pageSize,map);
    }

    /**修改商品状态*/
    @Override
    public boolean updateProductionStatusById(int pid,int status){
        return productBaseDomain.updateProductionStatusById(pid,status);
    }
    
    //-----------------------------------------

    /**
     * 修改商品orderSort
     * @param pid
     * @param orderSort
     * @param upOrDown  1上，2下
     * @return
     */
    @Override
    @Transactional
    public boolean updateProductOrderSort(int pid,int orderSort,int upOrDown) {
        //1、根据upOrDown获取替换目标记录
        Map m=productBaseDomain.getTargetChangeOrderSortProduct(pid,orderSort,upOrDown);
        if(m==null||m.size()==0)throw new ApolloException(MessageEnum.TARGET_PRODUCT_NOT_FOUND);
        int targId=Integer.parseInt(m.get("id")+"");
        int targOrderSort=Integer.parseInt(m.get("orderSort")+"");
        //2、替换两条记录的orderSort
        productBaseDomain.updateOrderSort(pid,targOrderSort);
        productBaseDomain.updateOrderSort(targId,orderSort);
        return true;
    }

    /**
     * 修改商品库存
     * @param pid
     * @param step 正加负减
     * @return
     */
    @Override
    public boolean  reduceInventoriesById(int pid,int step){
        return productBaseDomain.reduceInventoriesById(pid,step);
    }

    /**
     * 修改限购商品限购时间
     * @param pid
     * @return
     */
    @Override
    public boolean changeLimitProductTime(int pid, String startData, String endDate) {
        Product p=productBaseDomain.getProductById(pid);
        if(p==null||p.getStatus()==2||p.getIsLimit()==1)
            throw new ApolloException(MessageEnum.TARGET_PRODUCT_NOT_FOUND);
        return productBaseDomain.changeLimitProductTime(pid,startData,endDate);
    }

    /**限购商品模块状态 true显示 false隐藏
     * @return
     */
    @Override
    public boolean limitProductModuleStatus() {
        Map map = Maps.newHashMap();
        map.put("isLimit",2);
        map.put("isShowModule",0);
        int isShowModuleCount = productBaseDomain.selectCountByCondition(map);
        map.put("isShowModule",1);
        int isHideModuleCount = productBaseDomain.selectCountByCondition(map);
        if(isShowModuleCount==isHideModuleCount && isShowModuleCount==0){
            return false;
        }
        if(isShowModuleCount>isHideModuleCount){
            return true;
        }
        return false;
    }

    /**设置限时购模块是否显示
     * @param status
     * @return
     */
    @Override
    public boolean setLimitProductModuleStatus(boolean status) {
        if(status){
            productBaseDomain.updateProductIsShowModule(0);
        }else{
            productBaseDomain.updateProductIsShowModule(1);
        }
        return true;
    }
}
